package com.xiao.seckil.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.group.common.Vo.UsersEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xiao.seckil.entity.SeckillEntity;
import com.xiao.seckil.service.SeckillService;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.R;



/**
 * 秒杀表活动
 *
 * @author 韩国涛
 * @email 2845009006@gmail.com
 * @date 2021-09-29 09:12:32
 */
@RestController
@RequestMapping("seckil/seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = seckillService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取秒杀 的地址
     * @param usersEntity
     * @param goodsId
     * @return
     */
    @RequestMapping(value="/path",method= RequestMethod.GET)
    public R getPath(UsersEntity usersEntity,Integer goodsId){
        //判断用户 是否 不等于空
        if(usersEntity == null){
            return R.error("请登陆").put("data",false);
        }

        String path = this.seckillService.creatPath(usersEntity,goodsId);
        return R.ok().put("data",path);
    }

    //根据id查询是否存在
    @RequestMapping("toinfo")
    public boolean toinfo(@RequestParam("goodsId")Integer goodsId){
        SeckillEntity seckillEntity = seckillService.getOne(new QueryWrapper<SeckillEntity>().eq("goods_id", goodsId).ne("seckil_status", 0));
        if(seckillEntity ==null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{seckilId}")
    public R info(@PathVariable("seckilId") Integer seckilId){
		SeckillEntity seckill = seckillService.getById(seckilId);

        return R.ok().put("seckill", seckill);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SeckillEntity seckill){
		return seckillService.tosave(seckill);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SeckillEntity seckill){
		seckillService.updateById(seckill);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] seckilIds){
		seckillService.removeByIds(Arrays.asList(seckilIds));

        return R.ok();
    }

}
